package wang.hu.bos.web.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PictureAction extends ActionSupport {
	private File imgFile;
	private String imgFileFileName;
	private String imgFileContentType;

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	@Action(value = "picture_upload", results = { @Result(type = "json") })
	public String upload() throws IOException {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/files");
		System.out.println("文件存放路径：" + realPath);
		FileUtils.copyFile(imgFile, new File(realPath + "/" + imgFileFileName));
		String webPath = "http://localhost:8080/bos_management/files/"
				+ imgFileFileName;

		Map<String, Object> result = new HashMap<>();
		result.put("error", 0);
		result.put("url", webPath);
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	@Action(value = "picture_manage", results = { @Result(type = "json") })
	public String picmanage() throws IOException {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/files/");
		File directory = new File(realPath);
		List<Map<String, Object>> fileList = new ArrayList<>();
		if (directory.listFiles() != null) {
			for (File file : directory.listFiles()) {
				Map<String, Object> hash = new HashMap<>();
				String fileName = file.getName();
				if (file.isDirectory()) {
					hash.put("is_dir", true);
					hash.put("has_file", (file.listFiles() != null));
					hash.put("filesize", 0L);
					hash.put("is_photo", false);
					hash.put("filetype", "");
				} else {
					String fileExt = fileName.substring(
							fileName.lastIndexOf(".") + 1).toLowerCase();
					hash.put("is_dir", false);
					hash.put("has_file", false);
					hash.put("filesize", file.length());
					hash.put("is_photo", true);
					hash.put("filetype", fileExt);
				}
				hash.put("filename", fileName);
				hash.put("datetime",
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file
								.lastModified()));
				fileList.add(hash);
			}
		}

		Map<String, Object> result = new HashMap<>();
		result.put("moveup_dir_path", "");
		result.put("current_dir_path", realPath);
		result.put("current_url", ServletActionContext.getRequest()
				.getContextPath() + "/files/");
		result.put("total_count", fileList.size());
		result.put("file_list", fileList);

		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

}
